#ifndef CIRC_BUF_H
#define CIRC_BUF_H

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    unsigned int head;
    unsigned int tail;
    unsigned int size;
     uint8_t *buf;
} circ_buf_t;
#define MIN(a,b)     ((a)<(b)?a:b)
// Initialize or reinitialize a circular buffer
void circ_buf_init(circ_buf_t *circ_buf,  uint8_t *buffer, unsigned int size);

// Push a byte into the circular buffer
void circ_buf_push(circ_buf_t *circ_buf,  uint8_t data);

// Return a byte from the circular buffer
unsigned char circ_buf_pop(circ_buf_t *circ_buf);

// Get the number of bytes in the circular buffer
unsigned int circ_buf_count_used(circ_buf_t *circ_buf);

// Get the number of free spots left in the circular buffer
unsigned int circ_buf_count_free(circ_buf_t *circ_buf);

// Attempt to read size bytes from the buffer. Return the number of bytes read
unsigned int circ_buf_read(circ_buf_t *circ_buf,  uint8_t *data, unsigned int size);

// Attempt to write size bytes to the buffer. Return the number of bytes written
unsigned int circ_buf_write(circ_buf_t *circ_buf,   uint8_t *data, unsigned int size);

#ifdef __cplusplus
}
#endif

#endif